#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "testutilities.h"
#include <assert.h>


int testBaron(struct gameState *g){
	int buysBefore = g->numBuys;
	int handPos = searchHand(g, g->whoseTurn, baron);
	int handCount = g->handCount[g->whoseTurn];
	if(handPos == -1){
		g->hand[g->whoseTurn][g->handCount[g->whoseTurn]] = baron;
		handPos = g->handCount[g->whoseTurn];
		g->handCount[g->whoseTurn] ++;
	}
	int estateInSupply = g->supplyCount[estate];
	int estatesInDiscard = countCardsInSet(g, g->whoseTurn, estate, 3);
	int estatesInSet = countCardsInSet(g, g->whoseTurn, estate, 5);
	int estateCardInHand = searchHand(g, g->whoseTurn, estate);
	updateCoins(g->whoseTurn, g, 0);
	int coinsBefore = g->coins;
	int option1 = getRandomNumber(0, 1);
	int result = playCard(handPos, option1, -1, -1, g);
	//printf("result: %d", result);
	if(result < 0){
		return 0;
	}
	int buysAfter = g->numBuys;
	assert(buysAfter == buysBefore +1);
	int estatesInSetAfter = countCardsInSet(g, g->whoseTurn, estate, 5);
	int estatesInDiscardAfter = countCardsInSet(g, g->whoseTurn, estate, 3);
	updateCoins(g->whoseTurn, g, 0);
	int coinsAfter = g->coins;
	if(option1 == 0){
		if(estateInSupply){
			assert(estatesInSetAfter == estatesInSet +1);
		}
		assert(coinsBefore == coinsAfter);
	}
	else{
		//printf("choice: 1. In supply: %d In hand: %d coinsBefore: %d coinsAfter: %d\n", estateInSupply, estateCardInHand, coinsBefore, coinsAfter);
		assert(coinsAfter == coinsBefore + 4);
		assert(estatesInDiscardAfter == estatesInDiscard +1);
		//printf("inSetBefore: %d inSetAfter: %d handcount: %d\n", estatesInSet, estatesInSetAfter, handCount);
		assert(estatesInSet == estatesInSetAfter);
	}

	//printf("passed\n");
	return 1;
}


int main(int argc, char** argv){
	int tests = 1000;
	int seed = 1;

	if(argc > 1){
		tests = atoi(argv[1]);
	}
	if(argc > 2){
		seed = atoi(argv[2]);
	}
	prepRandom(1, seed);
	printf("Testing baron tests: %d seed: %d\n", tests, seed);
	int i;
	for(i=0; i < tests; i++){
		struct gameState *g = newGame();
		makeRandomGameState(g);
		testBaron(g);
		free(g);
	}

	printf("Tests Passed\n");

	return 0;
}
